/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myftp.app.action;

import java.io.FileNotFoundException;
import java.io.IOException;
import myftp.app.model.ServerManagerModel;
import myftp.app.model.ServerModel;
import myftp.app.model.ServerModel.TransferState;
import myftp.app.model.ServerTransferModel;
import myftp.app.model.TransfertTableModel;
import myftp.app.view.dialog.TemplateDialog;
import myftp.core.action.AbstractAction;
import myftp.core.controller.FrontController;

/**
 *
 * @author CAVARLE Glenn, LUCAS Stéphane
 * Reprend un transfert
 */
public class RestartAction extends AbstractAction {

    public void run() {

        /* Récupération des paramètres */
        Integer aTransfert = (Integer) this.getParam("number");
        TransfertTableModel aTransfertsTable = (TransfertTableModel) this.getParam("table");
        ServerManagerModel manager = (ServerManagerModel) this.getModel();
        ServerTransferModel transfer = null;
        ServerModel transferServer = null;
        String filePath = null;

        /* Demande de confirmation de reprise */
        Integer response = TemplateDialog.showRestartDialog();
        if (response == TemplateDialog.YES_OPTION) {

            /* Reprise du transfert */
            transfer = manager.getTransferAction(aTransfert);
            aTransfertsTable.restartTransfert(aTransfert);
            transferServer = transfer.getTransferServer();
            filePath = transfer.getPath() + transfer.getFilname();

            try {

                transferServer.hasTransfer(true);

                FrontController front;
                front = FrontController.getInstance();
                front.setActionParam("server", transferServer);
                front.setActionParam("table", aTransfertsTable);
                front.dispatch("TransferPromptAction");

                if (transfer.isUpload()) {
                    transfer.upload();
                } else if (transfer.isDownload()) {
                    transfer.download();
                } else {
                    throw new IOException("no dowload or upload action restarted");
                }
                TransferState state = transferServer.getStateTransfer();

                if (state.isPaused()) {
                    aTransfertsTable.pauseTransfert(transferServer.getConnectIndex());
                    transferServer.hasTransfer(false);
                } else if (state.isStoped()) {
                    aTransfertsTable.stopTransfert(transferServer.getConnectIndex());
                    transferServer.hasTransfer(false);
                    manager.deleteTransferAction(filePath);
                } else {
                    aTransfertsTable.closeTransfert(transferServer.getConnectIndex());
                    transferServer.hasTransfer(false);
                    manager.deleteTransferAction(filePath);
                }

                /* En cas de problèmes */
            } catch (FileNotFoundException ex) {
                aTransfertsTable.stopTransfert(transferServer.getConnectIndex());
                transfer.deleteBrokenFile(transfer.getFilname());
                transferServer.hasTransfer(false);
                manager.deleteTransferAction(filePath);
                TemplateDialog.showInternalErrorDialog(ex.getLocalizedMessage());
            } catch (IOException ex) {
                aTransfertsTable.stopTransfert(transferServer.getConnectIndex());
                transfer.deleteBrokenFile(transfer.getFilname());
                transferServer.hasTransfer(false);
                manager.deleteTransferAction(filePath);
                TemplateDialog.showInternalErrorDialog(ex.getLocalizedMessage());
            }
        }
    }

    public boolean init() {
        return true;
    }
}
